<?php

class Eleven_Resource_Teacher_Content extends CMS_Model_Resource_Db_Table_Abstract
{
	protected $_name = 'teachercontent';
	protected $_primary = 'id';
	protected $_rowClass = 'Eleven_Resource_Teacher_Content_Item';
	
	protected $_dependentTables = array('Eleven_Resource_Teacher_Reply');
	
	
	public function getContentsByTopicId($topicId, $paged = false, $order=array('updatetime DESC'))
	{
	    $topicId = (int) $topicId;
	    
	    $select = $this->select();
	    $select->where('topicid = ?', $topicId);
	    $order = array_merge(array('top DESC'), $order);
	    $select->order($order);
	
	    if (null !== $paged) 
	    {
			$adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
			$count = clone $select;
			$count->reset ( Zend_Db_Select::COLUMNS );
			$count->reset ( Zend_Db_Select::FROM );
			$count->from ( 'teachercontent', new Zend_Db_Expr ( 'COUNT(*) AS `zend_paginator_row_count`' ) );
			$adapter->setRowCount ( $count );
			
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 12 )->setCurrentPageNumber ( ( int ) $paged );
			return $paginator;
		}
		
	    return $this->fetchAll($select);
	}
	
	public function getContentById($id)
	{
	    return $this->find($id)->current();
	}
	
	public function deleteContentById($id)
	{
		return $this->find($id)->current()->delete();
	}
	
    public function setTops($tops, $isCancel=false)
	{
	    if (is_array($tops))
        {
            $where = $this->getAdapter()->quoteInto('id IN (?)', $tops);
            if ($isCancel)
            {
                $data = array(
                	'top'	=> 0,
                );   
            }else{
                $data = array(
                	'top'	=> 1,
                );
            }
            
            $this->update($data, $where);
        }
	}
}

?>